<?php
/**
 * 品牌模型
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
class BrandModel extends UE_Model {
    public $table  = 'ue_brand';
	public $key    = 'brand_id';
	
	public function add() {
		if (empty($brand['brand_name'])) {
			$this->setErr('品牌名称不能为空');
		    return false;
		}
		
		// logo
		$brandLogoInfo = array();
		if(!empty($_FILES['logo']['tmp_name'])) {
		    UE_Loader::loadModel('UploaderModel', 'system');
		    $uploader = new UploaderModel();
		    $uploader->setField('logo');
		    $brandLogoInfo = $uploader->add(array('upload_app'=>'product', 'upload_type' => 'brand'));
		    $brandLogoInfo && $brand['brand_logo'] = $brandLogoInfo['upload_id'];
		}
		
		$brandId = parent::add($brand);
		
		// 默认排序跟序号一样
		if(empty($brand['brand_order'])) {
			$sql = "UPDATE {$this->_table} SET brand_order=brand_id WHERE brand_id=$brandId LIMIT 1";
			UE_DB::factory()->query($sql);
		}
				
		// 关联栏目
		if(!empty($brand['cat_id'])) {
			foreach($brand['cat_id'] as $catId) {
				$sql = "INSERT INTO ue_brand_r_cat (brand_id, cat_id) VALUE ('{$brandId}', $catId)";
				UE_DB::factory()->query($sql);
			}
		}
		
		return $brandId;
	}
}